package com.example.bootcurriculumproject.testController;

import com.example.bootcurriculumproject.service.OrderService;
import com.example.bootcurriculumproject.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@Slf4j
public class testOrderController {
    @MockBean
    private OrderService orderService;
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private RedisUtil redisUtil;
    private static final String URL=  "/order";
    @Test
    public void testUpdateState() throws Exception {
        String body = "{\"consignmentnum\": \"aaaaaa\",\"addressnum\": 112233,\"states\": 0,\"goodno\": \"测试3\",\"buynumber\": 2,\"nowuid\": \"bbccdd\"}";
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.when(orderService.updateStatus(any())).thenReturn(true);
        String result = this.mockMvc.perform(MockMvcRequestBuilders.put(URL+"/state?consignmentnum=dbrntn")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        //.content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public void testDeleteAll() throws Exception {
        String body = "{\"consignmentnum\": \"aaaaaa\",\"addressnum\": 112233,\"states\": 0,\"goodno\": \"测试3\",\"buynumber\": 2,\"nowuid\": \"bbccdd\"}";
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.when(orderService.deleteAll(any())).thenReturn(true);
        String result = this.mockMvc.perform(MockMvcRequestBuilders.delete(URL+"/deleteAll?uid=7799")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        //.content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public void testDeleteOneOrder() throws Exception {
        String body = "{\"consignmentnum\": \"aaaaaa\",\"addressnum\": 112233,\"states\": 0,\"goodno\": \"测试3\",\"buynumber\": 2,\"nowuid\": \"bbccdd\"}";
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.when(orderService.deleteOne(any())).thenReturn(true);
        String result = this.mockMvc.perform(MockMvcRequestBuilders.delete(URL+"/deleteOne?consignmentnum=dsfe9")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        //.content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
    @Test
    public void testUpdateAddress() throws Exception {
        String body = "{\"consignmentnum\": \"aaaaaa\",\"addressnum\": 112233,\"states\": 0,\"goodno\": \"测试3\",\"buynumber\": 2,\"nowuid\": \"bbccdd\"}";
        //登录打桩
        Mockito.when(redisUtil.hasKey("nowUid")).thenReturn(true);
        Mockito.when(orderService.updateAddress(anyInt(),any())).thenReturn(true);
        String result = this.mockMvc.perform(MockMvcRequestBuilders.post(URL+"/updateAddress?address=112233&orderId=7799")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        //.content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
        assertEquals("true",result);
    }
}
